package ru.incoremedia.gaeutils.action;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Transaction;


public abstract class TransactionalAction<T> {
        
    public T execute() {

        DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

        Transaction transaction = datastoreService.beginTransaction();

        try {
            T result = doInTransaction();
            transaction.commit();
            return result;
        } catch(RuntimeException e) {
            transaction.rollback();
            throw e;
        }

    }
    
    public abstract T doInTransaction();

}
